package com.nytd.demo.controller;

import com.nytd.demo.entity.UserInfo;
import com.nytd.demo.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    private TransactionDefinition definition;
    @Autowired
    private UserInfoService userInfoService;

//    @RequestMapping("/t1")
//    public String registry1(UserInfo userInfo){
//        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(definition);
//        //⽤⼾注册
//        userInfoService.registryUser(userInfo);
//        dataSourceTransactionManager.commit(transactionStatus);
//        dataSourceTransactionManager.rollback(transactionStatus);
//        return "注册成功";
//    }

    @RequestMapping("/t1")
    @Transactional
    public String registry1(UserInfo userInfo){
        //⽤⼾注册
        userInfoService.registryUser(userInfo);
        log.info("用户数据插入成功");
        return "注册成功";

    }

    @RequestMapping("/t2")
    @Transactional
    public String registry2(UserInfo userInfo){
        //⽤⼾注册
        userInfoService.registryUser(userInfo);
        log.info("用户数据插入成功");
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "注册成功";

    }

    @RequestMapping("/t3")
    @Transactional
    public String registry3(UserInfo userInfo){
        //⽤⼾注册
        userInfoService.registryUser(userInfo);
        log.info("用户数据插入成功");
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            throw e;
        }
        return "注册成功";

    }

    @RequestMapping("/t4")
    @Transactional
    public String registry4(UserInfo userInfo){
        //⽤⼾注册
        userInfoService.registryUser(userInfo);
        log.info("用户数据插入成功");
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";

    }

    @RequestMapping("/t5")
    @Transactional
    public String registry5(UserInfo userInfo) throws IOException {
        //⽤⼾注册
        userInfoService.registryUser(userInfo);
        log.info("用户数据插入成功");
        if (true) {
            throw new IOException();
        }

        return "注册成功";

    }

    @RequestMapping("/t6")
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.DEFAULT)
    public String registry6(UserInfo userInfo) throws IOException {
        //⽤⼾注册
        userInfoService.registryUser(userInfo);
        log.info("用户数据插入成功");
        if (true) {
            throw new IOException();
        }

        return "注册成功";

    }

}
